package com.example.toolproject.demo.service.serviceimpl;

import com.example.toolproject.demo.domain.Answer;
import com.example.toolproject.demo.domain.Question;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.mapper.QuestionMapper;
import com.example.toolproject.demo.service.QuestionService;
import com.example.toolproject.demo.util.ResultBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName QuestionServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date
 **/
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public ResultBean addQuestion(Question question, Integer id) {
        question.setQuestionUserId(id);
        Integer addQuestionStatus=questionMapper.addQuestion(question);
        if(addQuestionStatus!=0){
            return ResultBean.success("成功添加问题");
        }else{
            return ResultBean.error("添加问题失败");
        }
    }

    @Override
    public ResultBean addAnswer(Answer answer, Integer id) {
        answer.setAnswerUserId(id);
        Integer addAnswerStatus=questionMapper.addAnswer(answer);
        if(addAnswerStatus!=0){
            return ResultBean.success("成功添加评论");
        }else{
            return ResultBean.error("添加评论失败");
        }
    }

    @Override
    public ResultBean getAnswer(Integer questionId) {
        List<Answer> questionList=questionMapper.getAnswer(questionId);
        return ResultBean.success(questionList);
    }

    @Override
    public ResultBean deleteAnswer(Integer answerId, User user) {
        Integer deleteAnswerStatus=questionMapper.deleteAnswer(answerId,user.getId(),user.getRoleId());
        if(deleteAnswerStatus!=0){
            return ResultBean.success("成功删除该评论");
        }else{
            return ResultBean.error("删除该评论失败,用户只能删除自己发布的评论");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBean deleteQuestion(Integer questionId) {
        Integer deleteQuestionStatus=questionMapper.deleteQuestion(questionId);
        questionMapper.deleteALLAnswer(questionId);
        if(deleteQuestionStatus!=0){
            return ResultBean.success("成功删除该问题");
        }else{
            return ResultBean.error("删除该问题失败");
        }
    }

    @Override
    public ResultBean getQuestion(Integer pageNum, Integer pageSize, String username, String school, String question) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questionList=questionMapper.getQuestion(username,school,question);
        questionList.forEach(question1 -> {
            List<Answer> answerList=questionMapper.getAnswer(question1.getQuestionId());
            if(answerList.size()==0){
                question1.setExists(false);
            }else{
                question1.setExists(true);
            }
        });
        PageInfo<Question> questionPageInfo=new PageInfo<>(questionList);
        return ResultBean.success(questionPageInfo);
    }
}
